package serial;

import java.io.*;

/**
 * @author zhangxin
 * @date 2020/9/22
 */
public class Test {
    Student student = new Student();

    public static void main(String[] args) {

        Student student = new Student();
        student.setName("Java");
        student.setAge(1);

        //write(student);

        read();
    }

    public static void read() {
        try {
            FileInputStream fileInputStream = new FileInputStream("d:\\1.txt");
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);

            Student student1 = (Student) objectInputStream.readObject();
            System.out.println(student1);
            fileInputStream.close();
            objectInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void write(Student student) {

        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("d:\\1.txt"));
            objectOutputStream.writeObject(student);
            objectOutputStream.flush();
            objectOutputStream.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public void test() {
        synchronized (student) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void test2() {
        synchronized (student) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized void test3() {
    }
}
